using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DotNetNukeMVPWizard.ProjectConfiguration
{
    class SafeIdentifierConfiguration : TextConfiguration
    {
        public SafeIdentifierConfiguration(string key)
            : base(key)
        {            
        }

        public SafeIdentifierConfiguration(string key, string errorReason)
            : base(key, errorReason)
        {            
        }

        public SafeIdentifierConfiguration(string key, string errorReason, bool isOptional)
            : base(key, errorReason, isOptional)
        {            
        }         

        public override string Value
        {
            get
            {
                return base.Value;
            }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    const string REPLACEABLE = @" @+^!~`';$()={}[]-,";
                    foreach (char item in REPLACEABLE.ToCharArray())
                    {
                        value = value.Replace(item, '_');
                    }
                    if (char.IsDigit(value[0]))
                        value = "_" + value;
                }
                base.Value = value;
            }
        }

        public override bool IsValid
        {
            get
            {
                bool isValid = base.IsValid;
                if (isValid && !string.IsNullOrEmpty(Value))
                {
                    if (Value.Equals(".") || Value.Equals(".."))
                        isValid = false;
                    else
                    {
                        const string DISALLOWED = "/?:&\\*\"<>|#%";
                        foreach (char item in DISALLOWED.ToCharArray())
                        {
                            if (Value.Contains(item))
                            {
                                isValid = false;
                                break;
                            }
                        }
                    }
                }
                return isValid;
            }
        }
    }
}
